/**
 * Created by chengcong on 2017/11/20.
 */

socket.on('connect', function(data) {
    console.log(socket.id);
    mysocketid = socket.id;
});
$(function () {
    var msg = window.sessionStorage['user_msg'];
    msg = JSON.parse(msg);
    socket.emit('login', {
        token:msg.token,
        username: msg.username,
        openid: null,
        sex: 0,
        marking: 1, //1pc  2wx  3app  Number
        userrole: msg.userrole //登陆角色 (医生(1)，用户(2)，助理(3)，药师(4),匿名用户(5))
    });
    socket.on('inviteroom', function(data) {
        console.log(data);
        window.top.document.getElementsByClassName('invitelist')[0].innerHTML =
            parseFloat(window.top.document.getElementsByClassName('invitelist')[0].innerHTML) + 1;
        window.top.document.getElementsByClassName('invitelist')[0].style.display = 'inline-block';
        $.ajax({
            url: url + '/service/inviteroom',
            type: 'post',
            data: {
                id: msg.userid,
                roomID: data.roomID,
                time: data.time,
                username: data.username,
                state: 0,
                socket_id: data.socket_id,
                roomname: data.roomname
            },
            headers: {
                'Authorization': 'Bearer ' + msg.token
            },
            success: function (d) {
            }
        })
    })
    socket.on('private_msg', function(data) {
        console.log(data);
        window.top.document.getElementsByClassName('unfinish')[0].innerHTML =
            parseFloat(window.top.document.getElementsByClassName('unfinish')[0].innerHTML) + 1;
        window.top.document.getElementsByClassName('unfinish')[0].style.display = 'inline-block';
    })
    socket.on('inviteroomss', function(data) {
        console.log(data);
        if(data.content == false) {
            window.top.document.getElementsByClassName('invitelist')[0].innerHTML =
                parseFloat(window.top.document.getElementsByClassName('invitelist')[0].innerHTML) - 1;
            if(parseFloat(window.top.document.getElementsByClassName('invitelist')[0].innerHTML) > 0) {
                window.top.document.getElementsByClassName('invitelist')[0].style.display = 'inline-block';
            } else {
                window.top.document.getElementsByClassName('invitelist')[0].style.display = 'none';
            }
        }
    });
    var asslist=[];
    $.ajax({
        url: url + '/server/designation_doctor',
        type: 'get',
        data: {
            _id:msg.userid
        },
        headers: {
            'Authorization': 'Bearer ' + msg.token
        },
        success: function (da) {
            console.log(da);
            asslist=da.data[0].myAssistant;
            $('tbody').html('');
            if(da.data[0].myAssistant.length>0){
                for(var i=0;i<da.data[0].myAssistant.length;i++){
                    var n1=0;
                    var n2=0;
                    var n3=0;
                    $.ajax({
                        url:url+'/service/find_Roominfo',
                        type:'post',
                        data:{
                            userid:da.data[0].myAssistant[i]._id
                        },
                        headers: {
                            'Authorization': 'Bearer ' + msg.token
                        },
                        success: function (d) {
                            console.log(d);
                            n1=d.content.length;

                        }
                    })
                    $.ajax({
                        url: url + '/server/assDocPending',
                        type: 'post',
                        data: {
                            id: da.data[0].myAssistant[i]._id,
                            userRole: 6
                        },
                        headers: {
                            'Authorization': 'Bearer ' + msg.token
                        },
                        success: function (d2) {
                            console.log(d2);
                            n2=d2.count;
                        }
                    })
                    $.ajax({
                        url: url + '/server/assDocFinish',
                        type: 'post',
                        data: {
                            id: da.data[0].myAssistant[i]._id,
                            userRole: 6
                        },
                        headers: {
                            'Authorization': 'Bearer ' + msg.token
                        },
                        success: function (d3) {
                            console.log(d3);
                            n3=d3.count;
                        }
                    })
                    $('tbody').append('<tr><td class="'+da.data[0].myAssistant[i]._id+'">'+
                        da.data[0].myAssistant[i].name+'</td><td>不在线</td><td>'+n1+'</td><td>'+n2+'</td><td><a href="finish.html?role='+da.data[0].myAssistant[i]._id+'">'+n3+'</a></td></tr>')
                }
                $.ajax({
                    url:url+'/service/onlindoctor',
                    type:'post',
                    headers: {
                        'Authorization': 'Bearer ' + msg.token
                    },
                    success:function (d) {
                        console.log(d);
                        var online=[];
                        for(let b=0;b<asslist.length;b++){
                            online[b]=false;
                            for (let a = 0; a < d.content.length; a++) {
                                if(d.content[a]._id==asslist[b]._id){
                                    online[b]=true
                                }

                            }

                        }
                        for (var k=0;k<online.length;k++){
                            if(online[k]==true){
                                $('.'+asslist[k]._id).next().text('在线');
                            }else {
                                $('.'+asslist[k]._id).next().text('不在线');
                            }
                        }
                    }
                });
            } else {
                $('tbody').append('<tr><td colspan="5"><img src="../../img/empty-b.png" alt="" style="margin-top: 200px"></td></tr>');
            }
            socket.on("userlogin", function (data) {
                doclist=data.userlist;
                var userinfo = data.userlist;
                console.log(userinfo);
                var html2 = "";
                console.log(data)
                if (data.userlist.length != 0) {
                    $.ajax({
                        url:url+'/service/onlindoctor',
                        type:'post',
                        headers: {
                            'Authorization': 'Bearer ' + msg.token
                        },
                        success:function (d) {
                            console.log(d);
                            var online=[];
                            for(let b=0;b<asslist.length;b++){
                                online[b]=false;
                                for (let a = 0; a < d.content.length; a++) {
                                    if(d.content[a]._id==asslist[b]._id){
                                        online[b]=true
                                    }

                                }

                            }
                            for (var k=0;k<online.length;k++){
                                if(online[k]==true){
                                    $('.'+asslist[k]._id).next().text('在线');
                                }else {
                                    $('.'+asslist[k]._id).next().text('不在线');
                                }
                            }
                        }
                    });
                }
            });
        }
    })
})
//审核不通过通知消息
socket.on("notthrough_msg",function(data){
    console.log(data.msg+data._id[0]);
    var bottom=-200;
    var timer=setInterval(function () {
        bottom+=4;
        window.top.document.getElementById('msgnew').style.bottom=bottom+'px';
        window.top.document.getElementsByClassName('newmsg')[0].innerHTML='审核不通过';
        if(bottom>0){
            clearInterval(timer);
        }
    },47)
    window.top.document.getElementsByClassName('unfinish')[0].innerHTML=
        parseFloat(window.top.document.getElementsByClassName('unfinish')[0].innerHTML)+1;
    window.top.document.getElementsByClassName('unfinish')[0].style.display='inline-block';
})
//撤回指派
socket.on("Undo", function(data2) {
    console.log(data2, 222)
    alert(data2.name+"撤回指派")
})
//撤回邀请
socket.on("UndoInvite", function(data) {
    window.top.document.getElementsByClassName('invitelist')[0].innerHTML =
        parseFloat(window.top.document.getElementsByClassName('invitelist')[0].innerHTML) - 1;
    if(parseFloat(window.top.document.getElementsByClassName('invitelist')[0].innerHTML) > 0) {
        window.top.document.getElementsByClassName('invitelist')[0].style.display = 'inline-block';
    } else {
        window.top.document.getElementsByClassName('invitelist')[0].style.display = 'none';
    }
});
